public class addBinnary_2 {
    public static void main(String[] args) {
        String str1 = "10111";
        String str2 = "10111";
        System.out.println(addBinary(str1,str2));
    }
    public static String addBinary(String binNum1, String binNum2){
        StringBuilder ret = new StringBuilder();
        int i = binNum1.length() - 1;
        int j = binNum2.length() - 1;
        int carry = 0;
        while(i >= 0 || j >= 0){
            int digit1 = i >= 0 ? binNum1.charAt(i--) - '0' : 0;
            int digit2 = j >= 0 ? binNum2.charAt(j--) - '0' : 0;
            int sum = digit1 + digit2 + carry;
            carry = sum >> 1;
            sum = sum >= 2 ? sum - 2 : sum;
            ret.append(sum);
        }
        if(carry == 1) ret.append(carry);
        return ret.reverse().toString();
    }
}
